package com.anche.cywebnew.Controller.systemManager;

import com.anche.cywebnew.DAO.systemManager.TerminalMapper;
import com.anche.cywebnew.Pojo.systemManager.SysUser;
import com.anche.cywebnew.Pojo.systemManager.TerminalInfo;
import com.anche.cywebnew.Service.securityManager.SecurityLogService;
import com.anche.cywebnew.Service.systemManager.TerminalService;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.ResultUtil;
import com.github.pagehelper.Page;
//import jdk.nashorn.internal.ir.Terminal;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/system/terminal")
public class TerminalController {

    private static final Logger logger = LoggerFactory.getLogger(TerminalController.class);

    @Autowired
    private TerminalService terminalService;

    @Autowired
    private SecurityLogService securityLogService;

    @Autowired
    private TerminalMapper terminalMapper;

    @RequestMapping("/manager")
    public String toTerminalPage() {
        return "/systemManager/terminal";
    }

    @GetMapping("/terminalPager")
    @ResponseBody
    public Object getTerminalTable(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, TerminalInfo terminal) {
        Map<String, Object> map = new HashMap<>();
        try {
            Page<TerminalInfo> terminalInfos = terminalService.getTerminalPager(page, limit, terminal);
            logger.info("<查询终端列表成功>");
            map.put("code", "0");
            map.put("count", terminalInfos.getTotal());
            map.put("msg", "查询成功");
            map.put("data", terminalInfos);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<查询终端列表异常:{}>", e);
            map.put("code", "1");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PutMapping("/islocked")
    @ResponseBody
    @OptLog(title = "终端解锁更新信息", type = OptType.UPDATE)
    public Object setIsLocked(@RequestParam("num_id") String num_id, @RequestParam("islocked") String islocked) {
        try {
            TerminalInfo temp = terminalMapper.getTerminalById(num_id);
            if (Objects.nonNull(temp)){
                terminalService.setIsLocked(num_id, islocked);
                SysUser curruser = (SysUser) SecurityUtils.getSubject().getPrincipal();
                securityLogService.insertSecurityLog("终端解锁", "终端【" + temp.getTerminal_id() + "】解锁成功", curruser);
                logger.info("<解锁终端[{}]成功>", num_id);
                return ResultUtil.success("解锁成功");
            }else {
                logger.error("<解锁终端[{}]失败：[{}]>", num_id, "终端不存在");
                return ResultUtil.error(0, "解锁终端失败：终端不存在");
            }

        } catch (Exception e) {
            logger.error("<解锁终端[{}]异常：[{}]>", num_id, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }
}
